package com.twosnail.frame.controller;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.twosnail.frame.commin.util.result.ResultObj;
import com.twosnail.frame.model.WxUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * Created by yangcs on 2017/8/30.
 */
public class AgentController  extends Controller {
    private Logger logger = Logger.getLogger(AgentController.class) ;

    /**
     * 按钮日志
     */
    @RequiresPermissions("AgentController")
    public void index(){
        try {
            Integer pageNum = getParaToInt( "pageNum" ) ;
            Integer numPerPage = getParaToInt( "numPerPage" ) ;
            String keyWord = getPara("keyWord") ;
            pageNum = pageNum == null ? 1 : pageNum;
            numPerPage = ( numPerPage == null || numPerPage == 0 ) ? 5 : numPerPage;

            Page<Record> list = WxUser.me.getAgents(pageNum,numPerPage,keyWord);
            setAttr( "list", list );
            setAttr( "keyWord", keyWord );
            setAttr( "levelStr", JSON.toJSONString(WxUser.me.getAgentLevels()));
        } catch (Exception e) {
            this.logger.warn( "用户列表信息，初始化失败！" , e );
        }
        render( "agent_list.html" );
    }

    /**
     * 下级代理商
     */
    public void childrenList(){
        String agentCode = getPara( "agentCode" ) ;
        try {
            setAttr("agentCode", agentCode);
            setAttr("list",WxUser.me.getAgents(agentCode));
            setAttr( "levelStr", JSON.toJSONString(WxUser.me.getAgentLevels()));
        } catch (Exception e) {
            this.logger.warn( "门店列表信息初始化失败！" , e );
        }
        render( "children_list.html" );
    }

    /**
     * 设置代理商
     */
    public void addAgent(){
        String mobile  = getPara( "mobile" ) ;
        String agentCode = getPara( "agentCode" ) ;
        try {
            if( mobile == null || "".equals( mobile ) ) {
                renderJson( new ResultObj( ResultObj.FAIL , "手机号不能为空！" , null ) );
            } else {
                WxUser wxUser = WxUser.me.getWxUser(mobile);
                if( wxUser == null ){
                    renderJson( new ResultObj( ResultObj.FAIL , "手机号未注册！" , null ) );
                }else if( wxUser.getStr("agentCode") != null && !"".equals(wxUser.getStr("agentCode")) ){
                    renderJson( new ResultObj( ResultObj.FAIL , "该用户已经是代理商！" , null ) );
                }else{
                    WxUser.me.addAgent(wxUser.getLong("wxUserId"),agentCode);
                    renderJson( new ResultObj( ResultObj.SUCCESS , "设置成功" , null ) );
                }
            }
        } catch (Exception e) {
            this.logger.warn( "系统异常，操作失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    public void updateAgentLevel(){
        long userId = getParaToLong("userId");
        int agentLevel = getParaToInt("agentLevel");
        try {
            WxUser.me.updateAgentLevel(userId,agentLevel);
            renderJson( new ResultObj( ResultObj.SUCCESS , "设置成功" , null ) );
        } catch (Exception e) {
            this.logger.warn( "系统异常，操作失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }
}
